#include "JDD/classes/DataManager.hpp"
#include <fstream>
namespace JDD
{
DataManager::DataManager()
{

}
DataManager::~DataManager()
{

}
void DataManager::LoadRaw(std::string theFilename)
{
    std::fstream anFile(theFilename.c_str(),std::ios::in|std::ios::ate);
    anFile.seekg(0,std::ios::beg);
    std::string anString, anSubString;
    char anBuffer[MAX_CHARS];
    bool isType=true;
    typeData anData;
    anData.FileName=theFilename;
    bool isString=false;
    while(!anFile.eof())
    {
        anData.Name="";
        anData.Values.clear();
        anFile.getline(anBuffer,MAX_CHARS);
        anString=anBuffer;
        for(unsigned int i=0; i<anString.length(); i++)
        {
            if(anString[i]==' ' || anString[i]==';')
            {
                if(!isString)
                {
                    if(isType)
                    {
                        anData.Name=anSubString;
                        isType=false;
                    }
                    else
                    {
                        anData.Values.push_back(anSubString);
                    }
                    anSubString="";
                }
                else if(isString && anString[i]==' ')
                {
                    anSubString+=anString.at(i);
                }
            }
            else if(anString[i]=='[')
            {
                isString=true;
            }
            else if(anString[i]==']')
            {
                isString=false;
            }
            else
            {
                anSubString+=anString.at(i);
            }
        }
        isType=true;
        mDataList.push(anData);
    }
}
void DataManager::SaveRaw(std::string theFilename)
{
    std::fstream anFile(theFilename.c_str(),std::ios::out);
    typeData anData;
    std::string anString;
    while(!mDataList.empty())
    {
        anData=mDataList.front();
        anString=anData.Name;
        anString+=" ";
        unsigned int i;
        for(i=0; i<anData.Values.size()-1; i++)
        {
            anString=anData.Values[i];
            anString+=" ";
        }
        anString=anData.Values[i];
        anString+="\r";
        anFile<<anString.c_str();
    }
}
typeData DataManager::PullData()
{
    typeData anData;
    if(mDataList.empty())
        return anData;
    anData=mDataList.front();
    mDataList.pop();
    return anData;
}
bool DataManager::isEmpty()
{
    return mDataList.empty();
}
}
